/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.player;

import cz.fi.muni.diplomka.common.enumeration.ColorType;
import cz.fi.muni.diplomka.common.enumeration.TitleType;
import cz.muni.fi.diplomka.servicelayer.dto.Player;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import cz.muni.fi.diplomka.servicelayer.util.Crypto;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class EditPlayerPage extends WebPage {

    private PlayerManager playerManager = new PlayerManagerImpl();
    String gender;
    String color;
    private static final List<String> GENDER = Arrays.asList(new String[]{
        "Sir", "Lady"});
    private static final List<String> COLOR = Arrays.asList(new String[]{
        "Red", "Yellow", "Green", "Blue"});

    public EditPlayerPage(final PageParameters parameters) {
        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));

            Player player = playerManager.findPlayerById(id);
            gender = player.getTitle().toString();
            color = player.getColor().toString();

            final RequiredTextField<String> nickName = new RequiredTextField<>("nickName", Model.of(player.getNickName()));

            final PasswordTextField password = new PasswordTextField("password", Model.of(""));

            final PasswordTextField retypePassword = new PasswordTextField("retypePassword", Model.of(""));

            final DropDownChoice<String> gender = new DropDownChoice<>("gender",
                    new PropertyModel<String>(this, "gender"), GENDER);

            final DropDownChoice<String> color = new DropDownChoice<>("color",
                    new PropertyModel<String>(this, "color"), COLOR);


            Form<?> form = new Form<Void>("form") {
                @Override
                protected void onSubmit() {
                    if (playerManager.findPlayerByLogin(nickName.getValue()).getId() != null && !playerManager.findPlayerByLogin(nickName.getValue()).getId().equals(id)) {
                        error("Name already taken");
                        return;
                    }
                    Player player = new Player();
                    player.setId(id);
                    player.setNickName(nickName.getValue());
                    try {
                        player.setHashedPassword(Crypto.encode(password.getValue()));
                    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
                        Logger.getLogger(RegistrationPage.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    switch (color.getValue()) {
                        case "0":
                            player.setColor(ColorType.Red);
                            break;
                        case "1":
                            player.setColor(ColorType.Yellow);
                            break;
                        case "2":
                            player.setColor(ColorType.Green);
                            break;
                        case "3":
                            player.setColor(ColorType.Blue);
                            break;
                    }
                    switch (gender.getValue()) {
                        case "0":
                            player.setTitle(TitleType.Sir);
                            break;
                        case "1":
                            player.setTitle(TitleType.Lady);
                            break;
                    }

                    playerManager.editPlayer(player);

                    info("Info has been changed");
                }
            };

            add(form);
            form.add(nickName);
            form.add(password);
            form.add(retypePassword);
            form.add(new EqualPasswordInputValidator(password, retypePassword));
            form.add(color);
            form.add(gender);
        }

    }
}